package com.powernode.ssm.utils;

import cn.hutool.core.bean.BeanUtil; 
import cn.hutool.core.util.ClassUtil; 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powernode.ssm.dao.*;
import com.powernode.ssm.pojo.po.*;
import com.powernode.ssm.pojo.bo.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.stereotype.Component; 

import java.lang.reflect.Field; 
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

@Component
@Slf4j
public class EmpListener extends AnalysisEventListener<ImportEmpBo> {

   private EmpDao empDao;
   private H5UserinfoPo h5UserinfoPo; 
   private int batchSize = getBatchSize(EmpPo.class);//ー次批量插入数据量
   private String msg = "";//是否有重复的提示
   List<EmpPo> list= new ArrayList<>();
   private int insertCount = 0;//重复的记录
   private int sumCount = 0;//导入的记录数

   public EmpListener(){
   }

   public EmpListener(EmpDao empDao, H5UserinfoPo h5UserinfoPo) {
       this.empDao = empDao;
       this.h5UserinfoPo =h5UserinfoPo;
   }

@SneakyThrows
@Override
public void invoke(ImportEmpBo importEmpBo, AnalysisContext analysisContext) {
       //如果上传文件中存在一个空的单元格,就要报错
       Field[] declaredFields = importEmpBo. getClass().getDeclaredFields();
       int nullNum = 0;
       for (Field declaredField : declaredFields) {
           declaredField.setAccessible(true);
           if (declaredField.get(importEmpBo)==null){
               nullNum++;
           }
       }
       //判断是否大于固定为nul1的字段数0
       if (nullNum>0){
           throw new NoSuchBeanDefinitionException("上传文件中不能存在空的单元格或未按照模板导入");
       }

       EmpPo empPo = BeanUtil.copyProperties(importEmpBo, EmpPo.class);

       list.add(empPo);
       sumCount++;
   }

   @Override
   public void doAfterAllAnalysed(AnalysisContext analysisContext) {

       log.info("{}的一次批量插入数据量为{}",
       ClassUtil.getClassName(this.getClass(),true), 
       batchSize);

       int size = list.size();

       if (size==0){
           throw new NoSuchElementException("上传文件格式错误或者数据为空");
       }


        //todo 根据业务需要去重

        ArrayList<EmpPo> subList = new ArrayList<>();

       //分批写入
       for (int i =0; i< list.size(); i++) {
           subList.add(list.get(i));
           if (subList.size() >= batchSize){
               int num1 = empDao.importEmp(subList);
               insertCount +=num1;
               subList.clear();
           }
       }


       //剩余全部写入
       if (!subList.isEmpty()){
           int num = empDao.importEmp (subList);
           insertCount +=num; 
           subList.clear();
       }

       list.clear();

       msg = String.format("本次导入数据【%d】条，存在重复数据【%d】条,过滤后最终导入的数据为【%d】条! ",sumCount, sumCount - insertCount, insertCount); 
       log.info("本次导入数据【{}】条,存在重复数据【{}】条,过滤后最终导入的数据为【{}】条!",sumCount, sumCount - insertCount, insertCount);


   }

   public String getMsg() {
       return msg;
   }

   public int getBatchSize(Class clazz){
       return 32767 / clazz.getDeclaredFields().length;
   }

}
